// pages/my/confirm.js
const app = getApp();
const action = require('../../utils/action.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    order: {},
    regions: [],
    customItem: '全部',
    test: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({id:options.id});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.loadModel();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  loadModel: function(){
    var self = this;
    var url = "/index.php?r=v1/order/index&id="+this.data.id;
    app.httpGet(url, {
      success: function(res){
        console.log(res);
        self.setData({order:res.data});
        self.setData({regions: res.data.address.regions });
      }
    });
  },
  confirmOrder: function(e){
    
  },
  editOrder: function(e){
    console.log(e);
    this.saveOrder(e);
  },
  formSubmit: function(e){
    console.log(e);
    this.saveOrder(e);
  },
  saveOrder: function(e){
    console.log(e.detail.value);
    var self = this;
    var order = this.data.order;
    var address = {
      name: e.detail.value.name,
      mobile: e.detail.value.mobile,
      regions: e.detail.value.regions,
      street: e.detail.value.street
    };
    order.address = address;
    this.setData({
      order: order
    });
    console.log(this.data.order);
    app.httpPost('/index.php?r=v1/order/confirm', {
      success: function (res) {
        if (res.code == 200) {
          console.log(res);
          self.setData({
            order: res.data
          });
          app.toast('保存成功');
        }
        else {
          app.toast(res.msg);
        }
      }
    }, self.data.order);
  },
  formReset: function(e){
    
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var order = this.data.order;
    this.setData({
      regions: e.detail.value
    });
  },
  doPayment: function (e) {
    var orderId = e.currentTarget.dataset.id;
    console.log(orderId);

    action.payOrder(this, orderId);
  }
})